package com.yy.util;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;

/**
 * Created by Administrator on 2015/6/3.
 */
public abstract class AppBaseActivity extends ActionBarActivity implements
        View.OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {

    public final static String REDIRECT_FLAG = "redirectFlag";

    protected LayoutInflater mInflater;
    protected ActionBar mActionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentView();
        int layoutId = getLayoutId();
        if(layoutId != 0) {
            setContentView(layoutId);
        }
        mInflater = getLayoutInflater();
        mActionBar = getSupportActionBar();
        commitFragment();
        View[] views = measureView();
        if (views != null && views.length > 0) {
            for (View v : views) {
                v.getViewTreeObserver().addOnGlobalLayoutListener(this);
            }
        }
    }

    @Override
    public void onClick(View v) {

    }

    public void redirect(Class<?> clazz) {
        redirect(null, clazz, false);
    }

    public void redirectAndFinish(Class<?> clazz) {
        redirect(null, clazz, true);
    }

    public void redirect(Bundle param, Class<?> clazz) {
        redirect(param, clazz, false);
    }

    public void redirectAndFinish(Bundle param, Class<?> clazz) {
        redirect(param, clazz, true);
    }

    protected abstract int getLayoutId();

    protected void onBeforeSetContentView() {

    };

    protected Fragment createFragment() {
        return null;
    };

    /**
     * 设置要测量的View
     * @return
     */
    protected View[] measureView() {
        return null;
    }

    /**
     * 由于在onCreate方法中获取宽高会出现全部为0的情况（整个布局还未测量完毕）
     * 故该方法会等布局测量完毕后返回组件宽高信息，应和measureView()；一同使用
     * @param width
     * @param height
     */
    protected void onStartMeasure(int[] width, int[] height) {

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onGlobalLayout() {
        View[] views = measureView();
        int[] widths = new int[views.length];
        int[] heights = new int[views.length];
        for(int i = 0; i < views.length; i++) {
            View v = views[i];
            widths[i] = v.getMeasuredWidth();
            heights[i] = v.getMeasuredHeight();
            v.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        onStartMeasure(widths, heights);
    }

    private void commitFragment() {
        final FragmentManager supportFragmentManager = getSupportFragmentManager();
        final FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
        final Fragment fragment = createFragment();
        if(fragment != null) {
            fragmentTransaction.add(fragment, fragment.getClass().getSimpleName());
            fragmentTransaction.commit();
        }

    }

    private void redirect(Bundle param, Class<?> clazz, boolean isFinish) {
        final Intent intent = new Intent(this, clazz);
        if(param != null) {
            final Bundle bundle = new Bundle();
            bundle.putAll(param);
            intent.putExtra(REDIRECT_FLAG, bundle);
        }
        startActivity(intent);
        if(isFinish) {
            finish();
        }
    }

}
